<?php
namespace app\channel\controller;

use app\common\controller\ChannelBase;
use app\common\model\Stat as Stats;
use app\common\model\Gameagent;
use app\services\Stat as State;
use think\facade\Session;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Stat extends ChannelBase
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 首页 订单统计
     * @return mixed
     */
    public function index()
    {
        $params = $this->request->param();
        $page = $params['page'] ?? 1;

        $stat = new Stats();
        $where = [];
        $where[] = ['cid', '=', Session::get('channel_id')];
        if(!empty($params['gid'])){
            $where[] = ['gid', '=', $params['gid']];
        }else{
            $params['gid'] = "";
        }
        if (!empty($params['starttime']) && !empty($params['endtime'])) {
            $where[]= ['stat_date','between time',[$params['starttime'],$params['endtime']]];
        }elseif (!empty($params['starttime'])) {
            $where[] = ['stat_date','>=',$params['starttime']];
        }elseif (!empty($params['endtime'])) {
            $where[] = ['stat_date','<=',$params['endtime']];
        }else{
            $starttime = date("Y-m-d",strtotime("-7 day"));
            $endtime = date("Y-m-d");
            $where[]= ['stat_date','between',[$starttime,$endtime]];
        }
        $list = $stat->getStatList($where, $page,100);

        // 统计
        $statData = $stat->field("sum(new_users) as new_users,sum(new_roles) as new_roles,sum(active_users) as active_users,
        sum(pay_amount) as pay_amount,sum(pay_users) as pay_users,sum(pay_times) as pay_times")
            ->where($where)
            ->paginate(100, false, ['page' => $page,'query' =>request()->param()])->toArray();
        // 初始化求和变量
        $sumData = [
            'new_users' => 0,
            'new_roles' => 0,
            'active_users' => 0,
            'pay_amount' => 0,
            'pay_users' => 0,
            'pay_times' => 0
        ];
        if(!empty($statData['data'][0])){
            $sumData = $statData['data'][0];
        }
        $total_user = $stat->sum("new_users");// 总注册用户数
//        $total_roles = $stat->where($where)->sum("new_roles");// 总注册角色数
        $total_amount = $stat->sum("pay_amount");// 总充值金额

        $this->assign("total_user",$total_user );
//        $this->assign("total_roles",$total_roles );
        $this->assign("total_amount",$total_amount);
        $this->assign("sumData",$sumData);


        $gameAgent = new Gameagent();
        $game_list = $gameAgent->getAgentlist($this->channel_id);
        $this->assign("game_list",$game_list);
        $this->assign("params",$params );
        // 转换为数组以便返回
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 首页 订单统计
     * @return mixed
     */
    public function days()
    {
        $params = $this->request->param();
        $page = $params['page'] ?? 1;

        $stat = new Stats();
        $where = [];
        $where[] = ['cid', '=', Session::get('channel_id')];
        if (!empty($params['starttime']) && !empty($params['endtime'])) {
            $where[]= ['stat_date','between time',[$params['starttime'],$params['endtime']]];
        }elseif (!empty($params['starttime'])) {
            $where[] = ['stat_date','>=',$params['starttime']];
        }elseif (!empty($params['endtime'])) {
            $where[] = ['stat_date','<=',$params['endtime']];
        }else{
            $starttime = date("Y-m-d",strtotime("-7 day"));
            $endtime = date("Y-m-d");
            $where[]= ['stat_date','between',[$starttime,$endtime]];
        }
        if(!empty($params['gid'])){
            $where[] = ['gid','=',$params['gid']];
        }
        $list = $stat->getStatDayList($where, $page,100);

        // 统计
        $statData = $stat->field("sum(new_users) as new_users,sum(new_roles) as new_roles,sum(active_users) as active_users,
        sum(pay_amount) as pay_amount,sum(pay_users) as pay_users,sum(pay_times) as pay_times")
            ->where($where)
            ->paginate(100, false, ['page' => $page,'query' =>request()->param()])->toArray();

        // 初始化求和变量
        $sumData = [
            'new_users' => 0,
            'new_roles' => 0,
            'active_users' => 0,
            'pay_amount' => 0,
            'pay_users' => 0,
            'pay_times' => 0
        ];
        if(!empty($statData['data'][0])){
            $sumData = $statData['data'][0];
        }
        $total_user = $stat->sum("new_users");// 总注册用户数
//        $total_roles = $stat->where($where)->sum("new_roles");// 总注册角色数
        $total_amount = $stat->sum("pay_amount");// 总充值金额

        $this->assign("total_user",$total_user );
//        $this->assign("total_roles",$total_roles );
        $this->assign("total_amount",$total_amount);
        $this->assign("sumData",$sumData);

        $this->assign("params",$params );
        $this->assign("action","index");
        $gameAgent = new Gameagent();
        $game_list = $gameAgent->getAgentlist($this->channel_id);
        $this->assign("game_list",$game_list);
        // 转换为数组以便返回
        return $this->fetch('days', ['list' => $list]);
    }

    // 玩家统计
    public function player()
    {
        return $this->fetch('index');
    }
}